/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.core.controlcenter;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;

import net.miginfocom.swing.MigLayout;

/**
 * @author Peter De Bruycker
 */
public abstract class ApplicationDialog {

    private JDialog dialog;
    private Window parent;
    private String title;

    public ApplicationDialog(Window parent, String title) {
        this.parent = parent;
        this.title = title;
    }

    protected JDialog createDialog(Window parent, String title) {
        JDialog dialog = new JDialog(parent, title);
        
        dialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                cancel();
            }
        });

        JPanel contentPanel = new JPanel(new MigLayout("wrap 1", "0[grow]0", "0[grow]0[]unrelated[]"));

        JComponent dialogContent = createDialogContent();
        if (dialogContent == null) {
            throw new IllegalStateException("createDialogContent cannot return null");
        }

        contentPanel.add(dialogContent, "grow");
        contentPanel.add(new JSeparator(JSeparator.HORIZONTAL), "grow");

        JButton okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                finish();
            }
        });
        contentPanel.add(okButton, "tag ok, split");

        AbstractAction cancelAction = new AbstractAction("Cancel") {
            @Override
            public void actionPerformed(ActionEvent e) {
                cancel();
            }
        };
        JButton cancelButton = new JButton(cancelAction);
        dialog.getRootPane().getActionMap().put("cancel", cancelAction);
        dialog.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"),
                "cancel");
        contentPanel.add(cancelButton, "tag cancel, split, gapright r");

        dialog.getRootPane().setDefaultButton(okButton);

        dialog.add(contentPanel);
        dialog.pack();
        
        dialog.setLocationRelativeTo(null);

        return dialog;
    }

    public void finish() {
        if (onFinish()) {
            dialog.setVisible(false);
        }
    }

    public void cancel() {
        onCancel();
        dialog.setVisible(false);
    }

    protected abstract boolean onFinish();

    protected abstract void onCancel();

    protected abstract JComponent createDialogContent();

    public void show() {
        if (dialog == null) {
            dialog = createDialog(parent, title);
            if (dialog == null) {
                throw new IllegalStateException("createDialog cannot return null");
            }
        }
        dialog.setVisible(true);
    }

    public void dispose() {
        dialog.dispose();
    }
}
